/*
 * Project  : VPC
 * Name   : ApplicationConstant.java
 *
 */
package com.virtium.vpc.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class ApplicationConstant extends Properties {
	private static Log log = LogFactory.getLog(ApplicationConstant.class);

	private static String filename = "";
    private static ApplicationConstant instance = null;

    public static String get(String s) {
    	try {
        	if (instance == null) {
        		instance = getInstance();
        	}
        	return instance.getProperty(s).trim();			
		} catch (Exception e) {
			System.out.println("Error: key " +  s + " not found in ApplicationConstant!");
    		log.info("Error getting constant string  value for "+s);
		}
	    return null;
    }
    
    public static int getInt(String s) {
    	int ret = -1;
    	try {
    		ret = Integer.parseInt(get(s));
    	} catch (NumberFormatException e) {
    		log.info("Error getting constant int value for "+s);
    	}
    	return ret;
    }
    
    public static synchronized ApplicationConstant getInstance() {
        if (instance == null) {
            try {
                instance = new ApplicationConstant();
                instance.load(new FileInputStream(new File(filename)));
                log.debug("Application Constants loaded");
            } catch (IOException e) {
                log.error("Cannot find " + filename, e);
            }
        }
        return instance;
    }

    public static synchronized void setFileName(String fname) {
        filename = fname;
        instance = ApplicationConstant.getInstance();
    }
}
